package taller2.controller.modes;

import java.awt.event.MouseEvent;

import taller2.interfaces.Graph;
import taller2.interfaces.Node;
import taller2.utils.Point;
import taller2.view.draws.DrawingArea;
import taller2.view.draws.GraphDrawer;

public class DeleteConnectionsMode implements Mode {
	private Graph graph;
	
	// constructor
	public DeleteConnectionsMode(Graph graph){
		this.graph = graph;
	}
	
	// when pressed selects a node, if a node is selected already
	// it deletes the connection between then (if there is one)
	public void whenPressed(MouseEvent arg0, DrawingArea area) {
		GraphDrawer drawer = area.getDrawer();
		Point pos = new Point(arg0.getX(),arg0.getY());
		
		if(drawer.noNodesSelected()){
			drawer.selectCloseByNode(pos);
			area.repaint();
		}else{
			int index1 = drawer.getSelectedNodeIndex();
			if(drawer.selectCloseByNode(pos)){
				int index2 = drawer.getSelectedNodeIndex();
				graph.deleteConnections(index1,index2);
				graph.deleteConnections(index2,index1);
				drawer.unselectNode();
				area.repaint();

				Node node1 = graph.getNodeByIndex(index1);
				node1.unselect();
			}
		}
	}

	public void whenReleased(MouseEvent arg0, DrawingArea area) {}

	public void whenDragged(MouseEvent arg0, DrawingArea area) {}

}
